//
//  RegisterViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/11/1.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "User.h"
#import "DataBaseSingleng.h"
@interface RegisterViewController ()

@property (nonatomic, retain)RegisterView *regVC;

@property (nonatomic, retain)User *userC;

@end

@implementation RegisterViewController

- (void)dealloc
{
    [_userC release];
    [_regVC release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.regVC = [[RegisterView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.regVC.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.regVC];
    [_regVC release];
    
    //设置标题
    self.navigationItem.title = @"注册";
    
    
    
    //设置导航栏右侧按钮(注册)
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBut:)];
    
    self.userC = [[User alloc]init];
    
}


//注册按钮方法
-(void)rightBarBut:(UIBarButtonItem *)but
{
    NSMutableArray *array = [[DataBaseSingleng shareDataBase]selectUser];
    NSMutableArray *array2 = [NSMutableArray array];
    
    for (User *user in array) {
        [array2 addObject:user.userName];
    }

    
    if ([self.regVC.nameFd.text isEqualToString:@""] || [self.regVC.passwordFd.text isEqualToString:@""]){
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入完整" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
    }else if ([array2 containsObject:self.regVC.nameFd.text ]) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已存在" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
    }else if (self.regVC.nameFd.text != nil && self.regVC.passwordFd.text != nil){
        
        
      
            
            User *user = [[User alloc]init];
            user.userName = self.regVC.nameFd.text;
            user.passworld = self.regVC.passwordFd.text;
            [[DataBaseSingleng shareDataBase]insertUser:user];
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
             [self.navigationController popViewControllerAnimated:YES];
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];

       
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
